package Chinese.Education.Identify.mapper;

import Chinese.Education.Identify.pojo.ScanResult;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ScanResultMapper {

    /**
     * 插入扫描结果
     * @param scanResult 扫描结果对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO scan_result(scan_name, target, start_time, end_time, status, description) " +
            "VALUES(#{scanName}, #{target}, #{startTime}, #{endTime}, #{status}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertScanResult(ScanResult scanResult);

    /**
     * 根据ID更新扫描结果
     * @param scanResult 扫描结果对象
     * @return 影响的行数
     */
    @Update("UPDATE scan_result SET scan_name=#{scanName}, target=#{target}, start_time=#{startTime}, " +
            "end_time=#{endTime}, status=#{status}, description=#{description} WHERE id=#{id}")
    int updateScanResult(ScanResult scanResult);

    /**
     * 根据ID删除扫描结果
     * @param id 扫描结果ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM scan_result WHERE id = #{id}")
    int deleteScanResult(Integer id);

    /**
     * 根据ID查询扫描结果
     * @param id 扫描结果ID
     * @return 扫描结果对象
     */
    @Select("SELECT id, scan_name as scanName, target, start_time as startTime, end_time as endTime, " +
            "status, description FROM scan_result WHERE id = #{id}")
    ScanResult selectScanResultById(Integer id);

    /**
     * 查询所有扫描结果
     * @return 扫描结果列表
     */
    @Select("SELECT id, scan_name as scanName, target, start_time as startTime, end_time as endTime, " +
            "status, description FROM scan_result")
    List<ScanResult> selectAllScanResults();

    /**
     * 根据扫描名称和状态查询扫描结果
     * @param scanName 扫描名称
     * @param status 状态
     * @return 扫描结果列表
     */
    @Select("<script>" +
            "SELECT id, scan_name as scanName, target, start_time as startTime, end_time as endTime, " +
            "status, description FROM scan_result " +
            "<where>" +
            "<if test='scanName != null and scanName != \"\"'> AND scan_name LIKE CONCAT('%', #{scanName}, '%')</if>" +
            "<if test='status != null and status != \"\"'> AND status = #{status}</if>" +
            "</where>" +
            "</script>")
    List<ScanResult> selectScanResultsByCondition(@Param("scanName") String scanName, @Param("status") String status);

    List<ScanResult> selectScanResult(@Param("limit") int limit,
                                      @Param("offset") int offset,
                                      @Param("scanName") String scanName,
                                      @Param("id") int id);

    int selectCount();
}

